package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.dao.mapper.web.GenreSiteMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.po.GenreSite;


/**
 * @author yuechao
 *
 */
public final class GenreSiteDao {
	private GenreSiteDao() {
	}
	
	public static List<GenreSite> selectAllGenreSites(){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreSiteMapper mapper = session.getMapper(GenreSiteMapper.class);
			QueryExample ex = new QueryExample();
			ex.setOrderByClause("version desc");
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
	
	public static boolean saveOrUpdateGenreSite(GenreSite genreSite){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			GenreSiteMapper mapper = session.getMapper(GenreSiteMapper.class);
			GenreSite temp = mapper.selectByPrimaryKey(genreSite.getId());
			int aff = 0;
			if(temp != null){
				aff = mapper.updateByPrimaryKey(genreSite);
			}else{
				aff = mapper.insert(genreSite);
			}
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static boolean deleteGenreSite(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			GenreSiteMapper mapper = session.getMapper(GenreSiteMapper.class);
			int aff = mapper.deleteByPrimaryKey(pk);
			return aff == 1;
		} catch (Exception e) {
			return false;
		}finally{
			session.close();
		}
	}
	
	public static final boolean updateGenreSiteByPK(GenreSite genreSite){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreSiteMapper mapper = session.getMapper(GenreSiteMapper.class);
			int aff = mapper.updateByPrimaryKey(genreSite);
			return aff == 1;
		}finally{
			session.close();
		}
	}
	
	public static final GenreSite getGenreSiteByPK(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreSiteMapper mapper = session.getMapper(GenreSiteMapper.class);
			GenreSite genreSite = mapper.selectByPrimaryKey(pk);
			return genreSite;
		}finally{
			session.close();
		}
	}
}
